package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Bean20;
import com.huike.game.base.Game20;
import com.huike.game.dao.Game20Dao;
import com.huike.game.fragment.Game20CoFragmentFragment;
import com.huike.game.fragment.Game20FragmentAirFragment;
import com.huike.game.fragment.Game20FragmentHumFragment;
import com.huike.game.fragment.Game20FragmentTemFragment;

import org.json.JSONObject;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class GameActivity20 extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private LineChart chart;
    private ViewPager vp_game20_vp;
    private List<Fragment> list;
    private TextView tv_game20_air;
    private TextView tv_gam20_tem;
    private TextView tv_gam20_hum;
    private TextView tv_game20_co;
    private RequestQueue queue;
    private ProgressDialog show;
    private ImageView iv_game20_refresh;
    private TextView tv_game20_ten;
    private TextView tv_game20_dayTem;
    private LineChart lc_game_chart;
    private TextView tv_game20_Ultraviolet_introduce;
    private TextView tv_game20_Ultraviolet_val;
    private TextView tv_game20_Ultraviolet_desc;
    private TextView tv_game20_cold_desc;
    private TextView tv_game20_cold_val;
    private TextView tv_game20_cold_introduce;
    private TextView tv_game20_dress_desc;
    private TextView tv_game20_dress_val;
    private TextView tv_game20_dress_introduce;
    private TextView tv_game20_exercise_desc;
    private TextView tv_game20_exercise_val;
    private TextView tv_game20_exercise_introduce;
    private TextView tv_game20_air_desc;
    private TextView tv_game20_air_val;
    private TextView tv_game20_air_introduce;
    private  Game20Dao dao;
    private List<Bean20> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game20);

        try {
            dao = new Game20Dao(GameActivity20.this);
            dataList = dao.allList();

        } catch (SQLException e) {
            e.printStackTrace();
        }


        queue = Volley.newRequestQueue(this);
        list = new ArrayList<>();
        list.add(new Game20FragmentAirFragment(dataList));
        list.add(new Game20FragmentTemFragment(dataList));
        list.add(new Game20FragmentHumFragment(dataList));
        list.add(new Game20CoFragmentFragment(dataList));
        initView();

//        3秒刷新
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                initData();
            }
        },0,3000);




        iv_game20_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                show();

                JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "lifeHelper", new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        hide();

                        Game20 bean = new Gson().fromJson(jsonObject.toString(), Game20.class);



                        tv_game20_ten.setText(bean.getData().getTem()+"°");
                        tv_game20_dayTem.setText("今天：" +bean.getData().getDayTem() +"°C");

                        if(bean.getData().getUltraviolet() <= 1000){
                            tv_game20_Ultraviolet_val.setText("弱("+bean.getData().getUltraviolet()+")");
                            tv_game20_Ultraviolet_introduce.setText("辐射较弱，涂擦SPF12~15、PA+护肤品");

                        }else if(bean.getData().getUltraviolet() >= 1000 && bean.getData().getUltraviolet() <= 3000){
                            tv_game20_Ultraviolet_val.setText("中等("  + bean.getData().getUltraviolet()+")");
                            tv_game20_Ultraviolet_desc.setText("涂擦SPF大于15、PA+防晒护肤品");

                        }else {
                            tv_game20_Ultraviolet_val.setText("强("  + bean.getData().getUltraviolet()+")");
                            tv_game20_Ultraviolet_desc.setText("尽量减少外出，需要涂抹高倍数防晒霜");
                        }



                        if(bean.getData().getCold() <= 8){
                            tv_game20_cold_introduce.setText("温度低，风较大，较易发生感冒，注意防护");
                            tv_game20_cold_val.setText("较易发("+bean.getData().getCold()+")");
                        }else{
                            tv_game20_cold_introduce.setText("无明显降温，感冒机率较低");
                            tv_game20_cold_val.setText("少发("+bean.getData().getCold()+")");
                        }

                        if(bean.getData().getDress() <= 12){
                            tv_game20_dress_val.setText("冷("+bean.getData().getDress()+")");
                            tv_game20_dress_introduce.setText("建议穿长袖衬衫、单裤等服装");
                        }else if(bean.getData().getDress() >= 12 && bean.getData().getDress() <= 21 ){
                            tv_game20_dress_val.setText("舒适("+bean.getData().getDress()+")");
                            tv_game20_dress_introduce.setText("建议穿短袖衬衫、单裤等服装");
                        }else{
                            tv_game20_dress_val.setText("热("+bean.getData().getDress()+")");
                            tv_game20_dress_introduce.setText("适合穿T恤、短薄外套等夏季服装");
                        }

                        if(bean.getData().getExercise() <= 3000){
                            tv_game20_exercise_val.setText("适宜(" +bean.getData().getExercise() +")");
                            tv_game20_exercise_introduce.setText("气候适宜，推荐您进行户外运动");
                        }else if(bean.getData().getExercise() >= 300 && bean.getData().getExercise() <= 6000){
                            tv_game20_exercise_val.setText("中(" +bean.getData().getExercise() +")");
                            tv_game20_exercise_introduce.setText("易感人群应适当减少室外活动");
                        }else{
                            tv_game20_exercise_val.setText("较不宜(" +bean.getData().getExercise() +")");
                            tv_game20_exercise_introduce.setText("空气氧气含量低，请在室内进行休闲运动");
                        }

                        if(bean.getData().getAir() <= 30){
                            tv_game20_air_val.setText("优(" + bean.getData().getAir() +")");
                            tv_game20_air_introduce.setText("空气质量非常好，非常适合户外活动，趁机出去多呼吸新鲜空气");
                        }else if(bean.getData().getAir() >= 30 && bean.getData().getAir() <= 100){
                            tv_game20_air_val.setText("良(" + bean.getData().getAir() +")");
                            tv_game20_air_introduce.setText("易感人群应适当减少室外活动");
                        }else{
                            tv_game20_air_val.setText("污染(" + bean.getData().getAir() +")");
                            tv_game20_air_introduce.setText("空气质量差，不适合户外活动");
                        }



                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("---",volleyError.getMessage());
                        hide();
                        Toast.makeText(GameActivity20.this, "数据加载失败！", Toast.LENGTH_SHORT).show();
                    }
                });

                queue.add(request);

            }
        });


        ViewPageHolder holder = new ViewPageHolder(getSupportFragmentManager());
        vp_game20_vp.setAdapter(holder);

        vp_game20_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                isSelect(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        tv_game20_air.setOnClickListener(this);
        tv_gam20_tem.setOnClickListener(this);
        tv_gam20_hum.setOnClickListener(this);
        tv_game20_co.setOnClickListener(this);

    }

    private void iniChart(Game20 bean) {


        //        图表
        chart.setDescription("");
        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.TOP);
        xAxis.setEnabled(true);

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setLabelCount(5, false);
        leftAxis.setEnabled(true);
        leftAxis.setDrawGridLines(true);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setLabelCount(5, false);
        rightAxis.setDrawGridLines(false);
        rightAxis.setEnabled(false);


        // set data
        chart.setData(generateDataLine(bean));

        // do not forget to refresh the chart
        // holder.chart.invalidate();
        chart.animateX(750);
    }

    private void show() {

        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();

    }

    private void hide() {
        show.dismiss();
    }

    private void initData() {


        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "lifeHelper", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Game20 bean = new Gson().fromJson(jsonObject.toString(), Game20.class);

                try {
                    Bean20 bean20 = new Bean20();
                    bean20.setAir(bean.getData().getAir());
                    bean20.setCo(bean.getData().getExercise());
                    bean20.setHum(bean.getData().getDress());
                    bean20.setTem(bean.getData().getTem());
                    bean20.setTime(new Date().getTime());
                    Log.e("---bean20", bean20.toString());
                    dao.save(bean20);
                } catch (SQLException e) {
                    Log.e("---", "Message");
                    Log.e("---",e.getMessage() +"getMessage");
                    e.printStackTrace();
                }


                tv_game20_ten.setText(bean.getData().getTem()+"°");
                tv_game20_dayTem.setText("今天：" +bean.getData().getDayTem() +"°C");

                if(bean.getData().getUltraviolet() <= 1000){
                    tv_game20_Ultraviolet_val.setText("弱("+bean.getData().getUltraviolet()+")");
                    tv_game20_Ultraviolet_introduce.setText("辐射较弱，涂擦SPF12~15、PA+护肤品");

                }else if(bean.getData().getUltraviolet() >= 1000 && bean.getData().getUltraviolet() <= 3000){
                    tv_game20_Ultraviolet_val.setText("中等("  + bean.getData().getUltraviolet()+")");
                    tv_game20_Ultraviolet_introduce.setText("涂擦SPF大于15、PA+防晒护肤品");

                }else {
                    tv_game20_Ultraviolet_val.setText("强("  + bean.getData().getUltraviolet()+")");
                    tv_game20_Ultraviolet_introduce.setText("尽量减少外出，需要涂抹高倍数防晒霜");
                }



                if(bean.getData().getCold() <= 8){
                    tv_game20_cold_introduce.setText("温度低，风较大，较易发生感冒，注意防护");
                    tv_game20_cold_val.setText("较易发("+bean.getData().getCold()+")");
                }else{
                    tv_game20_cold_introduce.setText("无明显降温，感冒机率较低");
                    tv_game20_cold_val.setText("少发("+bean.getData().getCold()+")");
                }

                if(bean.getData().getDress() <= 12){
                    tv_game20_dress_val.setText("冷("+bean.getData().getDress()+")");
                    tv_game20_dress_introduce.setText("建议穿长袖衬衫、单裤等服装");
                }else if(bean.getData().getDress() >= 12 && bean.getData().getDress() <= 21 ){
                    tv_game20_dress_val.setText("舒适("+bean.getData().getDress()+")");
                    tv_game20_dress_introduce.setText("建议穿短袖衬衫、单裤等服装");
                }else{
                    tv_game20_dress_val.setText("热("+bean.getData().getDress()+")");
                    tv_game20_dress_introduce.setText("适合穿T恤、短薄外套等夏季服装");
                }

                if(bean.getData().getExercise() <= 3000){
                    tv_game20_exercise_val.setText("适宜(" +bean.getData().getExercise() +")");
                    tv_game20_exercise_introduce.setText("气候适宜，推荐您进行户外运动");
                }else if(bean.getData().getExercise() >= 300 && bean.getData().getExercise() <= 6000){
                    tv_game20_exercise_val.setText("中(" +bean.getData().getExercise() +")");
                    tv_game20_exercise_introduce.setText("易感人群应适当减少室外活动");
                }else{
                    tv_game20_exercise_val.setText("较不宜(" +bean.getData().getExercise() +")");
                    tv_game20_exercise_introduce.setText("空气氧气含量低，请在室内进行休闲运动");
                }

                if(bean.getData().getAir() <= 30){
                    tv_game20_air_val.setText("优(" + bean.getData().getAir() +")");
                    tv_game20_air_introduce.setText("空气质量非常好，非常适合户外活动，趁机出去多呼吸新鲜空气");
                }else if(bean.getData().getAir() >= 30 && bean.getData().getAir() <= 100){
                    tv_game20_air_val.setText("良(" + bean.getData().getAir() +")");
                    tv_game20_air_introduce.setText("易感人群应适当减少室外活动");
                }else{
                    tv_game20_air_val.setText("污染(" + bean.getData().getAir() +")");
                    tv_game20_air_introduce.setText("空气质量差，不适合户外活动");
                }


                //初始化图片
                iniChart(bean);




            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("---",volleyError.getMessage());
                Toast.makeText(GameActivity20.this, "数据加载失败！", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }



    private void isSelect(int position) {
        if (position == 0) {

            tv_game20_air.setBackgroundResource(R.drawable.back_border_roun);
            tv_gam20_tem.setBackgroundResource(0);
            tv_gam20_hum.setBackgroundResource(0);
            tv_game20_co.setBackgroundResource(0);

        } else if (position == 1) {
            tv_game20_air.setBackgroundResource(0);
            tv_gam20_tem.setBackgroundResource(R.drawable.back_border_roun);
            tv_gam20_hum.setBackgroundResource(0);
            tv_game20_co.setBackgroundResource(0);

        } else if (position == 2) {
            tv_game20_air.setBackgroundResource(0);
            tv_gam20_tem.setBackgroundResource(0);
            tv_gam20_hum.setBackgroundResource(R.drawable.back_border_roun);
            tv_game20_co.setBackgroundResource(0);
        } else if (position == 3) {
            tv_game20_air.setBackgroundResource(0);
            tv_gam20_tem.setBackgroundResource(0);
            tv_gam20_hum.setBackgroundResource(0);
            tv_game20_co.setBackgroundResource(R.drawable.back_border_roun);
        }
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        chart = findViewById(R.id.lc_game_chart);
        vp_game20_vp = findViewById(R.id.vp_game20_vp);
        tv_game20_air = findViewById(R.id.tv_game20_air);
        tv_gam20_tem = findViewById(R.id.tv_gam20_tem);
        tv_gam20_hum = findViewById(R.id.tv_gam20_hum);
        tv_game20_co = findViewById(R.id.tv_game20_co);

        iv_game20_refresh = (ImageView) findViewById(R.id.iv_game20_refresh);
        tv_game20_ten = (TextView) findViewById(R.id.tv_game20_ten);
        tv_game20_dayTem = (TextView) findViewById(R.id.tv_game20_dayTem);
        lc_game_chart = (LineChart) findViewById(R.id.lc_game_chart);
        tv_game20_Ultraviolet_introduce = (TextView) findViewById(R.id.tv_game20_Ultraviolet_introduce);
        tv_game20_Ultraviolet_val = (TextView) findViewById(R.id.tv_game20_Ultraviolet_val);
        tv_game20_Ultraviolet_desc = (TextView) findViewById(R.id.tv_game20_Ultraviolet_desc);
        tv_game20_cold_desc = (TextView) findViewById(R.id.tv_game20_cold_desc);
        tv_game20_cold_val = (TextView) findViewById(R.id.tv_game20_cold_val);
        tv_game20_cold_introduce = (TextView) findViewById(R.id.tv_game20_cold_introduce);
        tv_game20_dress_desc = (TextView) findViewById(R.id.tv_game20_dress_desc);
        tv_game20_dress_val = (TextView) findViewById(R.id.tv_game20_dress_val);
        tv_game20_dress_introduce = (TextView) findViewById(R.id.tv_game20_dress_introduce);
        tv_game20_exercise_desc = (TextView) findViewById(R.id.tv_game20_exercise_desc);
        tv_game20_exercise_val = (TextView) findViewById(R.id.tv_game20_exercise_val);
        tv_game20_exercise_introduce = (TextView) findViewById(R.id.tv_game20_exercise_introduce);
        tv_game20_air_desc = (TextView) findViewById(R.id.tv_game20_air_desc);
        tv_game20_air_val = (TextView) findViewById(R.id.tv_game20_air_val);
        tv_game20_air_introduce = (TextView) findViewById(R.id.tv_game20_air_introduce);


        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
        tv_content_title.setText("生活助手");
        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);


    }

    private LineData generateDataLine(Game20 bean) {

        ArrayList<Entry> e1 = new ArrayList<Entry>();

//        for (int i = 0; i < 6; i++) {
//            e1.add(new Entry((int) (Math.random() * 65) + 40, i));
//        }

        e1.add(new Entry(bean.getData().getYesterday(),0));
        e1.add(new Entry(bean.getData().getNow(),1));
        e1.add(new Entry(bean.getData().getTomorrow(),2));
        e1.add(new Entry(bean.getData().getFir(),3));
        e1.add(new Entry(bean.getData().getSat(),4));
        e1.add(new Entry(bean.getData().getWeek(),5));

        LineDataSet d1 = new LineDataSet(e1, "");
        d1.setCircleSize(0.5f);
        d1.setColor(Color.parseColor("#F44336"));


        ArrayList<Entry> e2 = new ArrayList<Entry>();

//        for (int i = 0; i < 6; i++) {
//            e2.add(new Entry((int) (Math.random() * 65) + 40, i));
//        }
        e2.add(new Entry(bean.getData().getYesterday() + 30,0));
        e2.add(new Entry(bean.getData().getNow() + 30,1));
        e2.add(new Entry(bean.getData().getTomorrow() + 30,2));
        e2.add(new Entry(bean.getData().getFir() + 30,3));
        e2.add(new Entry(bean.getData().getSat() + 30,4));
        e2.add(new Entry(bean.getData().getWeek() + 30,5));

        LineDataSet d2 = new LineDataSet(e2, "");
        d2.setCircleSize(0.5f);
        d2.setColor(Color.parseColor("#3F51B5"));

        ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
        sets.add(d1);
        sets.add(d2);

        LineData cd = new LineData(getMonths(), sets);
        return cd;
    }

    private ArrayList<String> getMonths() {

        ArrayList<String> m = new ArrayList<String>();
        m.add("昨天");
        m.add("今天");
        m.add("明天");
        m.add("周五");
        m.add("周六");
        m.add("周日");

        return m;
    }

    @Override
    public void onClick(View view) {

        if (view == tv_game20_air) {

            vp_game20_vp.setCurrentItem(0);

        } else if (view == tv_gam20_tem) {
            vp_game20_vp.setCurrentItem(1);

        } else if (view == tv_gam20_hum) {
            vp_game20_vp.setCurrentItem(2);

        } else if (view == tv_game20_co) {
            vp_game20_vp.setCurrentItem(3);

        }
    }

    class ViewPageHolder extends FragmentStatePagerAdapter {

        public ViewPageHolder(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

    }

}
